<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input v-model="query.keyWord" placeholder="考试名称" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-select clearable filterable v-model="query.gradeType" placeholder="请选择年级">
          <el-option
            v-for="item in gradeTypeList"
            :key="item.id"
            :label="item.value"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select
          v-if="query.gradeType"
          clearable filterable
          v-model="query.subjectId"
          placeholder="请选择科目">
          <el-option
            v-for="item in subjectList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          v-if="hasPermission(['system:exam:list'])"
          @click="searchExamInfo"
          icon="el-icon-search" type="primary">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="examInfoList"
      border
      v-loading="loading"
      style="width: 100%;">

      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>

      <el-table-column
        prop="paper_name"
        align="center"
        min-width="200"
        label="考试名称">
      </el-table-column>

      <el-table-column
        prop="paper_mark"
        align="center"
        label="总分">
      </el-table-column>


      <el-table-column
        prop="grade_type"
        align="center"
        label="年级">
        <template slot-scope="scope">
          <el-tag size="small">{{scope.row.grade_type | getGradeName}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="subject_name"
        align="center"
        label="科目">
      </el-table-column>

      <el-table-column
        prop="create_date"
        align="center"
        :formatter="formatterCreateDate"
        label="考试时间">
      </el-table-column>

      <el-table-column
        prop="exam_number"
        align="center"
        label="参考人数">
      </el-table-column>

      <el-table-column
        prop="correct_number"
        align="center"
        label="已批改">
        <template slot-scope="scope">
          <el-tag type="success">{{scope.row.correct_number}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="no_correct_number"
        align="center"
        label="未批改">
        <template slot-scope="scope">
          <el-tag type="danger">{{scope.row.no_correct_number}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        min-width="180"
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="small"
            @click="lookStudentExam(scope.row)"
            type="primary">考试记录</el-button>
         <!-- <el-button
            size="small"
            @click="updateTestPaperInfo(scope.row)"
            type="warning">查看排名</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      @size-change="changeSize"
      @current-change="changePage"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  import {getGradeTypeListApi} from '@/utils/system-dict-api'
  let that = null
  export default {
    name: 'exam',
    data () {
      return {
        query: {
          gradeType: '',
          keyWord: ''
        },
        subjectList: [],
        examInfoList: [],
        loading: true,
        gradeTypeList: [],
        currentPage: 1,
        keyWord: '',
        pageSize: 10,
        totalCount: 0
      }
    },

    beforeCreate () {
      that = this
    },

    watch: {
      'query.gradeType' (val) {
        let that = this
        that.axios.get(that.$http.httpUrl('/system/subject'), {
          params: {
            sqlId: 'system.subject.list',
            gradeType: val
          }
        }).then(function (response) {
          that.subjectList = response.data.data
        }).catch(function (error) {
          console.log(error)
        })
      }
    },

    mounted () {
      that.getExamInfoList()
      getGradeTypeListApi(function (data) {
        that.gradeTypeList = data
      })
    },

    methods: {
      formatterCreateDate (row, column) {
        if (!row.create_date) {
          return '--'
        }
        return that.moment(row.create_date).format('YYYY-MM-DD HH:mm')
      },

      searchExamInfo () {
        that.currentPage = 1
        that.getExamInfoList()
      },

      lookStudentExam (exam) {
        sessionStorage.setItem('paper_name', exam.paper_name)
        this.$router.push({
          name: 'studentExam',
          query: {
            testPaperId: exam.test_paper_info_id,
            paper_mark: exam.paper_mark
          }
        })
      },

      changeSize (val) {
        that.pageSize = val
        that.currentPage = 1
        that.getExamInfoList()
      },

      changePage (val) {
        that.currentPage = val
        that.getExamInfoList()
      },
      getExamInfoList () {
        that.axios.get(that.$http.httpUrl('/system/exam'), {
          params: {
            keyWord: that.query.keyWord,
            subjectId: that.query.subjectId,
            gradeType: that.query.gradeType,
            offset: (that.currentPage - 1) * that.pageSize,
            limit: that.pageSize
          }
        }).then(function (response) {
          that.examInfoList = response.data.data.dataList
          that.totalCount = response.data.data.total
          that.loading = false
        })
      }
    },

    filters: {
      getGradeName (gradeType) {
        for (let i in that.gradeTypeList) {
          if (that.gradeTypeList[i].code === gradeType) {
            return that.gradeTypeList[i].value
          }
        }
      }
    }
  }
</script>

<style scoped>

</style>
